

import java.util.Scanner;
import java.util.InputMismatchException;

public class GameDriver 
{
	/**
	 * This program uses PlayingCard and Deck classes to complement the main
	 * method to play a game of video poker. It is a standard 5-card draw.
	 * 
	 * @author James Knight
	 * 
	 */

	public static void main(String[] args) 
	{
		Scanner input = new Scanner(System.in);
		CardDeck deck = new CardDeck();
		deck.shuffle(); //deck is unshuffled, so we call shuffle here
		Hand hand; 

		boolean continueGame = true;
		boolean betOkay = false;
		String playAgain;
		int number = 1;
		int count = 0;
		int bet = 0;
		int chip = 500;

		System.out.println("****************** Five Card Draw ********************************");
		System.out.println("**\t Text-based Implementation by Cheri Kau & James Knight \t**");
		System.out.println("**\t Game follows standard poker hands. Good luck! \t \t**");
		System.out.println("**\t You can discard up to three cards in this game. \t**");
		System.out.println("**\t They will be replaced with new cards from the deck. \t**");
		System.out.println("**\t The game will continue until you quit. \t \t**");
		System.out.println("**\t \t \t \t \t \t \t \t**");
		System.out.println("**\t Commands: \t \t \t \t \t \t**");
		System.out.println("**\t (1-5) will replace respective card. \t \t \t**");
		System.out.println("**\t (0) will cease discard process.\t \t \t**");
		System.out.println("**\t \t \t \t \t \t \t \t**");
		System.out.println("**\t Payouts: \t \t \t \t \t \t**");
		System.out.println("**\t Royal Flush \t \t \t 250:1\t \t \t**");
		System.out.println("**\t Straight Flush \t  \t 50:1\t \t \t**");
		System.out.println("**\t Four of A Kind \t \t 25:1\t \t \t**");
		System.out.println("**\t Full House \t \t \t 9:1\t \t \t**");
		System.out.println("**\t Flush \t \t \t \t 6:1\t \t \t**");
		System.out.println("**\t Straight \t \t \t 4:1\t \t \t**");
		System.out.println("**\t Three of A Kind \t \t 3:1\t \t \t**");
		System.out.println("**\t Two Pair \t \t \t 2:1\t \t \t**");
		System.out.println("**\t One Pair \t \t \t 1:1\t \t \t**");
		System.out.println("******************************************************************");

		//outer while loop to continue game until quit

		while (continueGame)
		{
			hand = new Hand(deck.deal(), deck.deal(), deck.deal(), deck.deal(), deck.deal());
			System.out.println("");
			System.out.print("Current Hand:    ");

			// for loop to handle displaying of card deck
			for (int i = 0; i < hand.getSize(); i++) 
			{
				if (i == 0) 
				{
					System.out.println("  Card " + (1 + i) + ": " + (hand.getCard(i)));
				} 
				else 
				{
					System.out.println("\t \t   Card " + (1 + i) + ": " + (hand.getCard(i)));
				}
			}
			System.out.println("");

			//start betting, will keep asking for bet until a valid one has been accepted
			System.out.println("You currently have " + chip + " chips.");

			while (betOkay == false)
			{
				System.out.println("Please enter your bet amount."); 

				try
				{
					bet = input.nextInt();
					if ((bet > chip)) 
					{
						System.out.println("You cannot bet more chips than you have!");
						betOkay = false;
					}
					else if (bet < 1) 
					{
						System.out.println("You entered " + bet + ".");
						System.out.println("Bet must be higher than 0.");
						betOkay = false;
					}
					else
					{
						System.out.println("Bet accepted. Good luck!");
						chip = chip - bet;
						betOkay = true;
					}
				} 
				catch (InputMismatchException ime) 
				{
					System.out.println("Please enter a valid number.");
				}	    	
			}


			//after bets, lets discard or complete round
			while (!(number == 0)) 
			{
				System.out.print("Enter the card number of the card you wish to" + " discard (or 0 to stop)");
				try 
				{
					number = input.nextInt();

					if (!(number == 0)) 
					{
						if (count < 2) 
						{
							System.out.println("You discarded: " + hand.replace(number, deck.deal()));
							System.out.println("Current Hand: " + (hand));
							count++;
						} 
						else 
						{
							number = 0;
						}
					}
				} 
				catch (InputMismatchException ime) 
				{
					System.out.println("Please try again and enter a value between 1 thru 5.");
				}

			}
			System.out.println("Your final hand: " + (hand));

			if(hand.isRoyalFlush() == true)
			{
				chip += bet * 250;
				System.out.println("Royal Flush!! You won " + chip + " chips!");
			}
			else if(hand.isStraightFlush() == true)
			{
				chip += bet * 50;
				System.out.println("Straight Flush!! You won " + chip + " chips!");
			}
			else if(hand.isFourOfAKind() == true)
			{
				chip += bet * 25;
				System.out.println("Four of a Kind!! You won " + chip + " chips!");
			}
			else if(hand.isFullHouse() == true)
			{
				chip += bet * 9;
				System.out.println("Full House!! You won " + chip + " chips!");
			}
			else if(hand.isFlush() == true)
			{
				chip += bet * 6;
				System.out.println("Flush!! You won " + chip + " chips!");
			}
			else if(hand.isStraight() == true)
			{
				chip += bet * 4;
				System.out.println("Straight!! You won " + chip + " chips!");
			}
			else if(hand.isThreeOfAKind() == true)
			{
				chip += bet * 3;
				System.out.println("Three of a Kind!! You won " + chip + " chips!");
			}
			else if(hand.isTwoPair() == true)
			{
				chip += bet * 2;
				System.out.println("Two Pair!! You won " + chip + " chips!");
			}
			else if(hand.isPair() == true)
			{
				chip += bet;
				System.out.println("One Pair!! You won " + chip + " chips!");
			}
			else
			{
				System.out.println("Your hand has no value. You lose.");
			}
			//check if player still have chips left
			if (chip == 0)
			{
				System.out.println("You are out of chips, try again!");
				System.exit(0);
			}
			else
			{
				System.out.println("Would you like to continue your game? (y/n)");
				input.nextLine(); //clear the buffer
				playAgain = input.nextLine();

				if (playAgain.equalsIgnoreCase("y")) 
				{
					continueGame = true;
					deck.shuffle();
				}
				else
				{
					System.out.println("Thanks for playing!");
					System.exit(0);
				}
			}

			//housekeeping, reset values in case player wants to continue
			number = 99; //any number here just to reset value
			betOkay = false;
			count = 0;

		}
	}
}
